package comp
{
	use namespace comp_layout;

	import comp.Interface.IDispose;
	import comp.style.css.CssAchieve;
	import comp.style.theme.ScrollBarTheme;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.utils.Dictionary;

	/**
	 *<h1> 面板控件</h1>
	 * 支持边框和背景，自动滚动条功能。</br>
	 * <h2>样式:</h2>
	 * 1.overflow :
	 * visible 默认值,内容不会被修剪，会呈现在元素框之外。 
 	 * hidden 内容会被修剪，并且其余内容是不可见的 不显示滚动条。 
	 * auto 如果内容被修剪，则浏览器会显示滚动条以便查看其余的内容。 
	 * scroll 显示滚动条</br>
	 * 2背景与边框:paddingLeft左填充，paddingRight填充,paddingTop上填充,paddingBottom下填充 
	 * backgroundImage背景图片,backgroundColor背景颜色,(backgroundImageX, backgroundImageY支持不太友好),backgroundRepeat(false|true)
	 * borderStyle("none")不为none就显示,borderThickness边框宽,borderColor边框颜色,cornerRadius(转角的半径);	</br>
	 * //增加resize()方法，支持动态改变大小
	 * </br>
	 * 
	 * 示例:子类:mvc.BaseView ,webgame.friends.Tips,webgame.mission.MisDialog所有View</br>
	 * 容器:webgame.friends.FriendsView 154行 
	 * a@uthor:pu</br>
	 * date:2010.8.24 9:53</br>
	 */
	public class Panel extends UIComponent
	{
		include "style/BorderStyles";//包含样式
		include "style/PaddingStyles";//包含样式
		include "style/SkinStyle" //皮肤属性
		//*********************
		//样式
		//*********************
		private var _layout:String;//="Basic";//布局方式 Basic,Vertical,Horizontal,TileLayout
		private var _overflow:String;//="visible";	//visible,hidden,scroll //,auto	
		private var _scrollbarMarginRight:Number;//=0;//滚动条X轴偏移
		private var _scrollbarMarginTop:Number;//=0;//滚动条X轴偏移
		private var _gap:int;//=0;//间格
		private var _tileWidth:Number;//=100;//格子宽
		private var _titleHeight:Number;//=100;//格子高
		private var _scrollExpand:Number;//扩展
		//public
		public function Panel()
		{
			super();
			this.addEventListener(MouseEvent.MOUSE_WHEEL,onWheelHandler);
			this._themeName="comp::Panel";
		}
		
		public function get titleHeight():Number
		{
			return _titleHeight;
		}
		
		public function set titleHeight(value:Number):void
		{
			_titleHeight = value;
		}
		
		
		public function get tileWidth():Number
		{
			return _tileWidth;
		}
		
		public function set tileWidth(value:Number):void
		{
			_tileWidth = value;
		}
		public function get gap():int
		{
			return _gap;
		}
		/**
		 * 仅会在布局元素之间应用该间隙，这样如果只有一个元素，则该间隙不会对布局有任何影响。
		 */
		public function set gap(value:int):void
		{
			_gap = value;
		}

		public function get layout():String
		{
			return _layout;
		}
		/** 
		 * Basic 类根据其各个设置彼此独立地排列布局元素。
		 * Vertical 类按垂直顺序从上向下排列布局元素，在元素和围绕元素顺序的可选填充之间带有可选间隙。
		 * Horizontal 类按水平顺序从左到右排列布局元素，在元素和围绕元素的可选填充之间带有可选间隙。
		 * TileLayout 类在单元格大小相等的列和行中排列布局元素。 
		 */
		public function set layout(value:String):void
		{
			_layout = value;
		}
		
		override public function set visible(value:Boolean):void
		{
			super.visible=value;
			if(_scrollbar)
				_scrollbar.visible=value;
		}
		
		public function get overflow():String
		{
			return _overflow;
		}
		/**
		 * 滚动条显示条件
		 * visible 默认值。内容不会被修剪，会呈现在元素框之外。 
 		 * hidden 内容会被修剪，并且其余内容是不可见的 不显示滚动条。 
		 * auto 如果内容被修剪，则浏览器会显示滚动条以便查看其余的内容。 
		 */
		public function set overflow(value:String):void
		{
			_overflow = value;
		}

		public function get label():String
		{
			return _label;
		}
		
		/**
		 * 相当于panel的标题不显示
		 */
		public function set label(value:String):void
		{
			_label = value;
		}

		public override function dispose():void
		{
			super.dispose();
			if(_scrollbar)
			{
				super.removeChild(_scrollbar);
				_scrollbar=null;					
			}
			this.currentSkin=null;
			if(this._closeBtn)
				_closeBtn.dispose();
			_closeBtn=null;
			if(this._titleUIComp)
				_titleUIComp.dispose();
			_titleUIComp=null;
			removeAllChild();
			for(var i:uint=super.numChildren;i>0;i--)
			{
				try{
					var dis:DisplayObject=super.removeChildAt(0);
					if(dis is IDispose)
					{
						(dis as IDispose).dispose();
						dis=null;	
					}
				}catch(e:Error){}
			}
			if(this.parent)
				parent.removeChild(this);
		}
		
		override public function styleChanged(styleProp:String, newValue:*):void
		{
			super.styleChanged(styleProp,newValue);
			if(styleProp=="head")
				this.head=newValue;
			else if(this.initialized && styleProp=="borderColor")
			{
				this.borderColor=newValue;
				this.merge();
			}
		}
		
		/**
		 * 删除所有子对像
		 */
		public function removeAllChild():void
		{
			for(var i:uint=this.numChildren;i>0;i--)
			{
				try{
					var dis:DisplayObject=this.removeChildAt(0);
					if(dis is IDispose)
					{
						(dis as IDispose).dispose();
						dis=null;	
					}
				}catch(e:Error){}
			}
		}
		
		public function resize(width:Number,height:Number):void
		{
			this.width=width;
			this.height=height;
			if(getStyleDefault("layout","basic").toLocaleLowerCase()!="basic")
				mergeLayout();
			else
				onSizeChangeHandle();
			showHead();
			this.graphics.clear();
			if(currentSkin)
			{
				currentSkin.width=width;
				currentSkin.height=height;
			}
		}

		/**
		 * 添加绝对定位的元素元素不受布局样式影响
		 */
		public function addChildAbsolute(child:DisplayObject):DisplayObject
		{
			return super.addChild(child);
		}
			
		public function get numChildrenAbsolute():int
		{
			return super.numChildren;
		}
		
		public function getChildAbsoluteAt(index:int):DisplayObject
		{
			return super.getChildAt(index);
		}		
		
		public function getChildAbsoluteByName(name:String):DisplayObject
		{
			return super.getChildByName(name);
		}
		
		public function getChildAbsoluteIndex(child:DisplayObject):int
		{
			return super.getChildIndex(child);
		}
		
		override public function addChild(child:DisplayObject):DisplayObject
		{
			addingChild(child);
			container.addChild(child);
			addedChild(child);
			if(initialized)
				merge();
			return  child;
		}
		
		/**
		 * 添加子对像</br>
		 * 首先调用addingChild();然后addChild(),最后addedChild()；
		 */
		override public function addChildAt(child:DisplayObject, index:int):DisplayObject
		{
			if(child==this.currentSkin)
				super.addChildAt(child,index);
			else				
			{
				addingChild(child);
				container.addChildAt(child,index);
				addedChild(child);
				if(this.initialized) //如果已经初使化需要计算合并
					merge();
			}			
			return  child;
		}
		
		override public function  getChildAt(index:int):DisplayObject
		{
			return container.getChildAt(index);
		}
		
		override public function getChildByName(name:String):DisplayObject
		{
			return container.getChildByName(name);
		}
		
		override public function getChildIndex(child:DisplayObject):int
		{
			return container.getChildIndex(child);
		}
		
		override public function get numChildren():int
		{
			return container.numChildren;
		}
		
		override public function removeChildAt(index:int):DisplayObject
		{
			return container.removeChildAt(index);
		}
		
		override public function removeChild(child:DisplayObject):DisplayObject
		{
			return container.removeChild(child);
		}
		
		override public function setChildIndex(child:DisplayObject, index:int):void
		{
			container.setChildIndex(child,index);
		}
	
		/**
		 * 大小改变的时候外部调用判断是否显示滚动条
		 */
		comp_layout function onSizeChangeHandle():void
		{
			
			if(this._autoH)
				super._myHeight=this.maxHeight;
			
			if(this._autoW)
				super._myWidth=this.maxWidth;
			
			if(this._autoH || this._autoW) //如果是自动宽高不显示滚动条
				return;		
			
			var overf:String=getStyleDefault("overflow","auto").toLocaleLowerCase();

			if(overf=="visible") //如果是不显示
				return;
			
			var pl:Number=getStyle("paddingLeft");
			var pt:Number=getStyle("paddingTop");
			var pr:Number=getStyle("paddingRight");
			var pb:Number=getStyle("paddingBottom");
			
			var containerheight:Number=height-pt-pb;
			var sexp:Number=this.getStyleDefault("scrollExpand",0);
			if(container.scrollRect==null)
				container.scrollRect=new Rectangle(-sexp,-sexp,width-pl-pr+sexp,containerheight);//如果指定了宽度
				
			if(overf=="scroll" || overf=="auto" )
			{
				if(containerheight<_maxHeight)
					showScroll();
				else
				{
					hideScroll();
					if(container.scrollRect!=null)
						container.scrollRect=null;//new Rectangle(0,0,width,height);//如果指定了宽度
				}
			}
		}
		
		override public function stylesInitialized():void
		{
			super.stylesInitialized();
			showHead();
		}
		
		override protected function merge():void
		{
			mergeLayout();
			CssAchieve.mergeBackGrounde(this);
			CssAchieve.mergeTemplate(this);
			mergeHead();
		}
		
		/**
		 *按layout属性布局并判断是否显示滚动条
		 */
		protected function mergeLayout():void
		{
			var lay:String=getStyleDefault("layout","basic");//getStyle("layout");
			var pl:Number=getStyle("paddingLeft");
			var pt:Number=getStyle("paddingTop");
			var pr:Number=getStyle("paddingRight");
			var pb:Number=getStyle("paddingBottom");
			var gap:Number=getStyle("gap");//间隔
			var containerheight:Number=height-pt-pb;
			var cwidth:Number=width-pr-pl;//内容宽度
			var lx:Number=0,ly:Number=0;//上个对像x和上个对像y
			var i:uint=0;
			
			container.x=pl;
			container.y=pt;
				
			var child:DisplayObject;
			var tMax:Number=0;
			var maxw:Number=0;
			var cheight:Number=0;
			var cwidth:Number=0;
			
			if(lay.toLocaleLowerCase()=="basic")//基本 类根据其各个设置彼此独立地排列布局元素。
			{
				var s:*;
				var numch:uint=this.container.numChildren;
				for(var i:uint=0;i<numch;i++)
				{
					s=container.getChildAt(i);
					if(s is UIComponent)
					{
						if(s.visible==true) 
						{
							cheight=s.y+(s.height==0?s.spriteHeight:s.height);
							cwidth=s.x+(s.width==0?s.spriteWidth:s.width);
						}
						//cheight=s.y+(s.maxHeight==0?s.spriteHeight:s.maxHeight);
					}
					else if(s.visible==true)
					{
						cheight=s.y+s.height;
						cwidth=s.x+s.width;
					}
					
					if(tMax<=cheight)
						tMax=cheight;
					
					if(maxw<=cwidth)
						maxw=cwidth;
					
				}
				
				_maxHeight=tMax+pb+pt;			
				_maxWidth=maxw+pr+pl;
			}
			else if(lay.toLocaleLowerCase()=="vertical") //垂直类按垂直顺序从上向下排列布局元素，在元素和围绕元素顺序的可选填充之间带有可选间隙。
			{
				for(i=0;i<this.numChildren;i++)
				{
					child=this.getChildAt(i);
					if(child!=currentSkin && child.visible==true)// ||  !(child is  ScrollBar)
					{
						child.x=lx;
						child.y=ly;	

						lx=child.x;							
						ly=child.y+child.height+gap;
						
						cwidth=child.x+child.width;
						
						if(maxw<=cwidth)
							maxw=cwidth;
					}
				}
				
				this._maxHeight=ly+pb+pt;
				this._maxWidth=maxw+pr+pl;

			}		
			else if(lay.toLocaleLowerCase()=="tilelayout")//tile方式
			{
				var th:Number=getStyle("titleHeight");
				var tw:Number=getStyle("tileWidth");
				for(i=0;i<this.numChildren;i++)
				{
					child=this.getChildAt(i);
					if(child!=currentSkin  && child.visible==true)// ||  !(child is  ScrollBar)
					{
						child.x=lx;
						child.y=ly;	
						
						if((child.x+child.width)>=(width-pr-pl) && !_autoW)//如果宽度超出
						{
							lx=0;
							ly=child.y+th+gap;
						}
						else
						{
							ly=child.y;
							lx=child.x+tw+gap;
							
							cwidth=child.x+child.width;							
							if(maxw<=cwidth)
								maxw=cwidth;
						}
					}//end if
					
				}//end for
				this._maxHeight=ly+pb+pt+th;
				this._maxWidth=maxw+pr+pl;

			}	
			else //(lay.toLocaleLowerCase()=="horizontal")//水平顺序
			{
				var ch:Number=0;
				for(i=0;i<this.numChildren;i++)
				{
					child=this.getChildAt(i);
					ch=child.height;
					if(child!=currentSkin  && child.visible==true)// || !(child is ScrollBar)
					{
							child.x=lx;
							child.y=ly;	
						
						if((child.x+child.width)>=(width-pr-pl) && !_autoW)//如果宽度超出
						{
							lx=0;
							ly=child.y+child.height+gap;
						}
						else
						{
							ly=child.y;
							lx=child.x+child.width+gap;
						}
						
						cwidth=child.x+child.width;
						
						if(maxw<=cwidth)
							maxw=cwidth;
					}
				}//end for
				this._maxHeight=ly+pb+pt+ch;
				this._maxWidth=maxw+pr+pl;

			}
			
			onSizeChangeHandle();
		}
		
		/**
		 * 滚轮事件
		 */
		protected function onWheelHandler(e:MouseEvent):void
		{
			if(_scrollbar)
				_scrollbar.scroll(-e.delta*2);
		}
		/**
		 * 创建滚动条
		 */
		protected function showScroll():void
		{
			var sexp:Number=this.getStyleDefault("scrollExpand",0);
			if(!_scrollbar && container.scrollRect!=null)
			{
				_scrollbar=new ScrollBar;
				_scrollbar.height=container.scrollRect.height;
				_scrollbar.maxHeight=_maxHeight+sexp;
				_scrollbar.tag=this.container;	
				_scrollbar.className=this.getStyle("scrollBarClass");					
				_scrollbar.onScroll=function(n:Number):void{
					var r:Rectangle=this.tag.scrollRect;
					if(r)
					{
						r.y=n-sexp;
						var r1:Rectangle=r;
						this.tag.scrollRect=r1;
					}
					
				}

				_scrollbar.x=this.width-this.getStyle("scrollbarMarginRight");
				_scrollbar.y=this.getStyle("scrollbarMarginTop");
				super.addChildAt(_scrollbar,0);
			}
			
			if(_scrollbar)
			{
				var yy:Number=_scrollbar.scrollY;
				var n:Number=yy*(_scrollbar.maxHeight/(_maxHeight+sexp));

				_scrollbar.height=container.scrollRect.height;
				_scrollbar.visible=true;
				_scrollbar.x=this.width-this.getStyle("scrollbarMarginRight");
				_scrollbar.maxHeight=_maxHeight+sexp;
				_scrollbar.onSizeChange();
				
				_scrollbar.scroll(n);
				
				super.setChildIndex(_scrollbar,super.numChildren-1);
			}
			
		}
		
		/**
		 * 显示头部信息
		 */
		protected function showHead():void
		{
			var h:String=this.getStyleDefault("head","both");
			if(h.toLocaleLowerCase()=="none")
				return;
			else if(h.toLocaleLowerCase()=="both") //都显示
			{
				if(this.label)
				{
					createTitle();
					this.addChildAbsolute(_titleUIComp);
				}
				
				createCloseBtn();
				this.addChildAbsolute(_closeBtn);
			}else if(h.toLocaleLowerCase()=="close")
			{
				createCloseBtn();
				this.addChildAbsolute(_closeBtn);
			}
		}
		
		protected function mergeHead():void
		{
			if(_titleUIComp)
			{
				_titleUIComp.x=(this.width-_titleUIComp.width)/2
				_titleUIComp.y=_titleUIComp.getStyle("marginTop");
			}
			
			if(_closeBtn)
			{
				_closeBtn.x=(this.width-_closeBtn.width)-_closeBtn.getStyle("marginRight");
				_closeBtn.y=_closeBtn.getStyle("marginTop");
			}
		}
		
		private function createTitle():void
		{
			if(!_titleUIComp)
			{
				_titleUIComp=new Label();	
				_titleUIComp.themeName=this._themeName+".Label";
			}
			var clname:String=this.getStyle("labelClassName");
			if(clname)
				_titleUIComp.className=clname;
			
			var style:Object=this.getStyle("labelStyle");

			if(style)
				_titleUIComp.style=style;
			
			if(_titleUIComp.hasOwnProperty("text"))
				_titleUIComp["text"]=label;
			_titleUIComp.initialize();
		}
		
		private function createCloseBtn():void
		{
			if(!this._closeBtn)
				_closeBtn=new Button();
			
			_closeBtn.className=this.getStyle("closeClassName");
			_closeBtn.style=this.closeStyle;
			_closeBtn.themeName=this.getStyle("themeName")+".Button";
			_closeBtn.clickFunction=closeFun;
			_closeBtn.initialize();
		}
		
		protected function closeFun(e:MouseEvent):void
		{
			if(_closeFunction!=null)
				_closeFunction(e);
			else
				this.dispose();
		}
		
		public function get container():Sprite
		{
			if(_container==null)
			{
				_container=new Sprite();
				super.addChild(_container);
			}
			return _container;
		}
		/**
		 * 隐藏滚动条
		 */
		protected function hideScroll():void
		{
			if(_scrollbar)
				_scrollbar.visible=false;
		}

		
		//private
		private var _head:String="both";//显示标题类型 none都不显示 both都显示 title只显示title close只显示closeBtn 
		private var _label:String;
		protected var _closeBtn:Button;//关闭按钮
		protected var _titleUIComp:UIComponent;//自定义title控件
		private var _labelStyle:Object;//label的样式
		private var _closeStyle:Object;//close button的样式
		private var _labelClassName:String;//label的样式
		private var _closeClassName:String;//close button的样式
		private var _closeFunction:Function;//关闭按钮事件
		
		private var _scrollBarClass:String;//滚动条样式
		protected var _scrollbar:ScrollBar;//滚动条
		private var _maxWidth:Number=0;

		/**
		 * 最大宽
		 */
		public function get maxWidth():Number
		{
			return _maxWidth;
		}

		/**
		 * 滚动条X
		 */
		public function get scrollbarMarginRight():Number
		{
			return _scrollbarMarginRight;
		}

		public function set scrollbarMarginRight(value:Number):void
		{
			_scrollbarMarginRight = value;
		}

		public function get scrollbarMarginTop():Number
		{
			return _scrollbarMarginTop;
		}

		public function set scrollbarMarginTop(value:Number):void
		{
			_scrollbarMarginTop = value;
		}

		/**
		 * 
		 * 显示标题类型 none都不显示 both都显示(默认) title只显示title close只显示closeBtn 
		 */
		public function get head():String
		{
			return _head;
		}

		/**
		 * @private
		 */
		public function set head(value:String):void
		{
			_head = value;
		}

		public function get labelStyle():Object
		{
			return _labelStyle;
		}

		public function set labelStyle(value:Object):void
		{
			_labelStyle = value;
		}

		/**
		 * close button的样式
		 */
		public function get closeStyle():Object
		{
			return _closeStyle;
		}

		/**
		 * @private
		 */
		public function set closeStyle(value:Object):void
		{
			_closeStyle = value;
		}

		/**
		 * label的样式名
		 */
		public function get labelClassName():String
		{
			return _labelClassName;
		}

		/**
		 * @private
		 */
		public function set labelClassName(value:String):void
		{
			_labelClassName = value;
		}

		/**
		 * close button的样式名
		 */
		public function get closeClassName():String
		{
			return _closeClassName;
		}

		/**
		 * @private
		 */
		public function set closeClassName(value:String):void
		{
			_closeClassName = value;
		}

		/**
		 * 自定义title控件 如果不指定默认显示Label
		 */
		public function get titleUIComp():UIComponent
		{
			return _titleUIComp;
		}

		/**
		 * @private
		 */
		public function set titleUIComp(value:UIComponent):void
		{
			_titleUIComp = value;
		}

		/**
		 * 关闭按钮事件
		 * function(e:MouseEvent):void
		 */
		public function get closeFunction():Function
		{
			return _closeFunction;
		}

		/**
		 * @private
		 */
		public function set closeFunction(value:Function):void
		{
			_closeFunction = value;
		}

		/**
		 * 滚动条样式
		 */
		public function get scrollBarClass():String
		{
			return _scrollBarClass;
		}

		/**
		 * @private
		 */
		public function set scrollBarClass(value:String):void
		{
			_scrollBarClass = value;
		}

		public function get scrollExpand():Number
		{
			return _scrollExpand;
		}

		/**
		 * 滚动区域扩展
		 * 
		 */
		public function set scrollExpand(value:Number):void
		{
			_scrollExpand = value;
		}

		
		private var _container:Sprite;//内容容器

	}
}